from peewee import *
from .db import BaseModel
from models import utils


class Flag(BaseModel):
    key = CharField(unique=True)
    value = CharField()

    @classmethod
    def get_ticker_last_date(self, ticker):
        Flag.select().where(Flag.key == 'last_date_' + ticker)

    @classmethod
    def set_ticker_last_date(self, ticker, dt):
        key = 'last_date_' + ticker
        flags = Flag.select().where(Flag.key == key)
        flag = flags[0] if flags else Flag.create(key=key)
        flag.value = utils.date2str(dt) if isinstance(dt, datetime) else dt
        return flag.save()
